<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="../ct-results-panel.html">

<link rel="import" href="../../model/ct-step-failure.html">

<script>
(function () {

var assert = chai.assert;

var kExampleFailures = [
  new CTStepFailure("testName", "plugins/gesture-events-scrolled.html",
    {
      "WebKit Win7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "time": 0.9
      },
      "WebKit Mac10.6 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 1.8
      },
      "WebKit Mac10.7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 3.2
      }
    },
    177164, 177165
  ),
  new CTStepFailure("testName", "plugins/transformed-events.html",
    {
      "WebKit Win7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "time": 0.6
      },
      "WebKit Mac10.6 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 1.4
      },
      "WebKit Mac10.7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 3
      }
    },
    177164, 177165
  ),
  new CTStepFailure("testName", "plugins/gesture-events.html",
    {
      "WebKit Win7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "time": 1.7
      },
      "WebKit Mac10.6 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 2.5
      },
      "WebKit Mac10.7 (dbg)": {
        "expected": "PASS",
        "is_unexpected": true,
        "actual": "CRASH",
        "has_stderr": true,
        "time": 3.4
      }
    },
    177164, 177165
  ),
];

describe('ct-results-panel', function() {
  var panel;

  function createPanel() {
    panel = document.createElement('ct-results-panel');
    // FIXME: The results-panel needs to be appended to the DOM so that the flakiness
    // dashboard's iframe has a window when we grab location off of it, but that
    // also means that we actually do fetches for the iframes of the foo_step stdio.
    // This results in some console spam from the 404'ed request.
    panel.hidden = true;
    document.body.appendChild(panel);
  }

  function assignFailures(failures) {
    // FIXME: This should be a proper CTFailureGroup
    panel.group = {
      data: {
        failures: failures
      }
    };
  }

  function assignUrl(url) {
    // FIXME: This should be a proper CTFailureGroup
    panel.group = {
      data: {
        url: url
      }
    };
  }

  afterEach(function() {
    panel.remove();
    panel = undefined;
  });

  it('should show no results', function(done) {
    createPanel();
    setTimeout(function() {
      var message = panel.shadowRoot.querySelector('.message');
      assert.equal(message.textContent, 'Loading...');

      panel.group = 'this is an invalid group';

      setTimeout(function() {
        var message = panel.shadowRoot.querySelector('.message');
        assert.equal(message.textContent, 'No results to display.');
        done();
      });
    });
  });

  it('should show tests and results for selected test', function(done) {
    createPanel();
    assignFailures(kExampleFailures);

    setTimeout(function() {
      assert.notOk(panel.shadowRoot.querySelector('.message'));

      var items = panel.shadowRoot.querySelectorAll('core-menu > div');
      assert.lengthOf(items, 3);
      assert.equal(items[0].textContent, 'plugins/gesture-events-scrolled.html');
      assert.equal(items[1].textContent, 'plugins/transformed-events.html');
      assert.equal(items[2].textContent, 'plugins/gesture-events.html');

      var results = panel.shadowRoot.querySelectorAll('ct-results-by-builder');
      assert.lengthOf(results, 1);
      assert.equal(results[0].failure, kExampleFailures[0]);

      panel.shadowRoot.querySelector('core-menu').selected = 2;

      setTimeout(function() {
        var results = panel.shadowRoot.querySelectorAll('ct-results-by-builder');
        assert.lengthOf(results, 1);
        assert.equal(results[0].failure, kExampleFailures[2]);

        done();
      });
    });
  });

  it('should show step failure', function(done) {
    createPanel();
    var failure = {
      testName: null,
      step: 'foo-step',
      resultNodesByBuilder: {
        'WebKit Win7 (dbg)': {
          actual: 'UNKNOWN',
        },
      },
      oldestFailingRevision: 123,
      newestPassingRevision: 124,
    };
    assignFailures([failure]);

    setTimeout(function() {
      assert.notOk(panel.shadowRoot.querySelector('.message'));

      var items = panel.shadowRoot.querySelectorAll('core-menu > div');
      assert.lengthOf(items, 0);

      var results = panel.shadowRoot.querySelectorAll('ct-results-by-builder');
      assert.lengthOf(results, 1);
      assert.equal(results[0].failure, failure);

      assert.lengthOf(panel.shadowRoot.querySelectorAll('ct-embedded-flakiness-dashboard'), 0);
      done();
    });
  });

  it('should show url failure', function(done) {
    createPanel();
    var url = 'https://url.com';
    assignUrl(url);

    setTimeout(function() {
      assert.notOk(panel.shadowRoot.querySelector('.message'));

      var items = panel.shadowRoot.querySelectorAll('core-menu > div');
      assert.lengthOf(items, 0);

      var results = panel.shadowRoot.querySelectorAll('ct-popout-iframe');
      assert.lengthOf(results, 1);
      assert.equal(results[0].src, url);

      assert.lengthOf(panel.shadowRoot.querySelectorAll('ct-embedded-flakiness-dashboard'), 0);
      done();
    });
  });

});

})();
</script>
